//
//  TopicViewController.swift
//  HoneyBee
//
//  Created by point on 2018/1/24.
//  Copyright © 2018年 point. All rights reserved.
//

import UIKit
import RxCocoa
import RxSwift
import MJRefresh


private let kCellID = "kCellID"
class TopicViewController: UIViewController {
    @IBOutlet weak var tableView: UITableView!
    
    fileprivate lazy var topicViewModel: TopicViewModel = TopicViewModel()
    override func viewDidLoad() {
        super.viewDidLoad()
        setupUI()
        // Do any additional setup after loading the view.
    }
}

extension TopicViewController:UITableViewDelegate,UITableViewDataSource {
    func numberOfSections(in tableView: UITableView) -> Int {
        return 1
    }
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return topicViewModel.modelObserable.count
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: kCellID, for: indexPath) as! TopicTableViewCell
        cell.configModel(model: topicViewModel.modelObserable[indexPath.item], index: indexPath)
        cell.selectionStyle = .none
        cell.deleteBlock = { [weak self] in
            let deleteConfirmVC =  TopicDeleteViewController()
            self?.navigationController?.pushViewController(deleteConfirmVC, animated: true)
            deleteConfirmVC.topicModel = (self?.topicViewModel.modelObserable[indexPath.row])!;
            deleteConfirmVC.deletedBlock = {(isSuccess) in
                if (isSuccess){
                    self?.topicViewModel.modelObserable.remove(at: indexPath.row )
                    self?.tableView.reloadData()
                }
            }
        }
        return cell
    }
    
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return 100
    }
    
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        
        tableView.deselectRow(at: indexPath, animated: false)
        
        let model = topicViewModel.modelObserable[indexPath.item]
        let vc = TopicDetailViewController()
        vc.configModel(model: model);
        self.navigationController?.pushViewController(vc, animated: true)
    }
}

extension TopicViewController {
    fileprivate func setupUI(){
        
        tableView.register(UINib(nibName: "TopicTableViewCell", bundle: nil), forCellReuseIdentifier: kCellID)
        tableView.separatorStyle = .singleLine
        tableView.tableFooterView = UIView(frame: CGRect.zero);
        
        topicViewModel.vc = self
        tableView.mj_header = MJRefreshNormalHeader(refreshingBlock: {
            self.topicViewModel.publishSubData.onNext(.mpLoadDataUp)
        })
        tableView.mj_footer = MJRefreshAutoNormalFooter(refreshingBlock: {
            self.topicViewModel.publishSubData.onNext(.mpLoadDataDown)
        })
        tableView.mj_header.beginRefreshing()

    }
    
}

